Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extract adapters support for dora and loha #1611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xiaoyu-work
Copy link
Contributor

Describe your changes

Add extract adapters support for dora and loha.

Checklist before requesting a review

  • Add unit tests for this change.
  • Make sure all tests can pass.
  • Update documents if necessary.
  • Lint and apply fixes to your code by running lintrunner -a
  • Is this a user-facing change? If yes, give a description of this change to be included in the release notes.
  • Is this PR including examples changes? If yes, please remember to update example documentation in a follow-up PR.

(Optional) Issue link

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this condition in the peft export helper

or getattr(module, "use_dora", {}).get(module.active_adapters[0], False)
?

Otherwise, the lora scaling factors appears in the graph and would need to be extracted also since it is not the same for all adapters.

output + default (lora_A) -> MatMul -> ...
output + default_1 (lora_B) -> MatMul -> ...

DoRA:
Copy link
Contributor

@jambayk jambayk Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the exported graph for a dora model after the scaling change above. the lora matmuls appear twice in the computation.
Also, the Add weight doesn't need to be extracted. It is just the transpose of the base weight and comes from this step https://github.com/huggingface/peft/blob/363c14e673a12d19f951609d06221962d5c3eb2a/src/peft/tuners/lora/dora.py#L78. This is constant for a model.

What needs to be extracted is the weight in the Div node that depends on the lora weights.
https://github.com/huggingface/peft/blob/363c14e673a12d19f951609d06221962d5c3eb2a/src/peft/tuners/lora/dora.py#L86
https://github.com/huggingface/peft/blob/363c14e673a12d19f951609d06221962d5c3eb2a/src/peft/tuners/lora/dora.py#L63

Image

Copy link
Contributor

@jambayk jambayk Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, during export, we can precompute https://github.com/huggingface/peft/blob/main/src/peft/tuners/lora/dora.py#L70-L86 so that ConstantOfShape -> Reshape doesn't appear in the graph (more compute + memory because of the duplicated transposed base weight) and we just have the mag_norm_scale weight directly in the graph. But I think this can be done later if needed and is out of scope for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants